package com.hxzy.controller.vue.login;

import com.hxzy.common.constans.RedisConstans;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.R;
import com.hxzy.common.util.JwtTokenService;
import com.hxzy.common.util.MarketingThreadLocalUtil;
import com.hxzy.common.util.MultipleTokenService;
import com.hxzy.common.util.UniqueTokenService;
import com.hxzy.controller.vue.login.req.AdminLoginReq;
import com.hxzy.controller.vue.login.resp.AdminLoginResp;
import com.hxzy.service.VueLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/8/29 9:13
 * @Version 1.0
 */
@RequestMapping(value = "/api")
@RestController
@Api(tags = "后台-登录")
public class AdminLoginController {

    @Qualifier(value = "vueLoginServiceImpl")
    @Autowired
    private VueLoginService  vueLoginService;

    @Autowired
    private RedisTemplate  redisTemplate;

    /**
     * 一个用户可以登录多次
     */
    @Autowired
    private MultipleTokenService multipleTokenService;

    /**
     * 一个用户只允许登录一次
     */
    @Autowired
    private UniqueTokenService uniqueTokenService;

    /**
     * 单次登录还是多次登录
     */
    @Value(value = "${token.unique}")
    private boolean tokenUnique;



    @ApiOperation(value = "登录")
    @PostMapping(value = "/login")
    public R<String>   login(@RequestBody @Valid AdminLoginReq req){
       //验证码
       String redisKey= RedisConstans.getRedisKey(RedisConstans.CAPTCHA_KEY,req.getUuid());
       Object redisValue=this.redisTemplate.opsForValue().get(redisKey);
       //从redis中移出验证码
       this.redisTemplate.delete(redisKey);

       //验证码不存在
       if(Objects.isNull(redisValue)){
           return R.build(AckCode.CODE_NOT_FOUND);
       }

       //验证码对不对
        if(!redisValue.toString().equalsIgnoreCase(req.getCode())){
            return R.build(AckCode.CODE_ERROR);
        }

         //走登录流程
        AdminLoginResp  adminLoginResp=this.vueLoginService.login(req);
        //存储到redis中，并生成jwt标识
        String adminJwtToken="";
        //一个账号可以多次登录 false 用户可以登录多次，true用户只允许登录一次
        if(this.tokenUnique){
            adminJwtToken=uniqueTokenService.createJwtToken(adminLoginResp);
        }else{
            adminJwtToken=multipleTokenService.createJwtToken(adminLoginResp);
        }

        return R.okHasData(adminJwtToken);
    }


    /**
     * 获取用户信息
     * @return
     */
    @ApiOperation(value = "获取用户信息")
    @GetMapping(value = "/user/info")
    public R<AdminLoginResp> userInfo(){
        AdminLoginResp loginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        return R.okHasData(loginResp);
    }

    /**
     * 退出
     * @return
     */
    @ApiOperation(value = "退出")
    @PostMapping(value = "/logout")
    public R logout(){
        AdminLoginResp loginResp =MarketingThreadLocalUtil.adminThreadLocal.get();
        if(this.tokenUnique){
            uniqueTokenService.delLoginUser(loginResp);
        }else{
            multipleTokenService.delLoginUser(loginResp);
        }

        return R.ok();
    }

}
